﻿// // ******************************************************************
// //       /\ /|       @file       JellyfinService.cs
// //       \ V/        @brief      await29
// //       | "")       @author     Await, 285283010@qq.com
// //       /  |
// //      /  \\        @Modified   2024-12-31 20:12
// //    *(__\_\        @Copyright  Copyright (c) , 2024-12-08 10:12 Await
// // ******************************************************************

using AutomaticSubtitles.Contracts.Dto.Jellyfin;
using AutomaticSubtitles.Service.Application.Jellyfin.Queries;
using AutomaticSubtitles.Service.Application.Jellyfin.Queries.User;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
using Masa.BuildingBlocks.Authentication.Identity;

namespace AutomaticSubtitles.Service.Services;

// [Route("api/[controller]")]
public class JellyfinService : BaseService<JellyfinService>
{
    private readonly IConfiguration _configuration;
    private readonly IHttpClientFactory _httpClientFactory;

    public JellyfinService(IHttpClientFactory httpClientFactory, IConfiguration configuration)
    {
        _httpClientFactory = httpClientFactory;
        _configuration = configuration;
    }

    /// <summary>
    /// 获取用户视图列表
    /// </summary>
    [EndpointDescription("获取用户视图列表")]
    [EndpointName("获取用户视图列表")]
    [EndpointSummary("获取用户视图列表")]
    [Authorize]
    public async Task<ResponseModel<JellyfinUserViewsDto>> GetUserViews(string id)
    {
        var query = new GetJellyfinUserViewsQuery(id);
        await PublishAsync(query);
        return query.Result.CreateResultAdd_Msg();
    }

    /// <summary>
    /// 获取媒体项列表
    /// </summary>
    [Authorize]
    [EndpointDescription("获取媒体项列表")]
    [EndpointName("获取媒体项列表")]
    [EndpointSummary("获取媒体项列表")]
    public async Task<ResponseModel<JellyfinItemsDto>> GetItems(string id)
    {
        var query = new GetJellyfinItemsQuery(id);
        await PublishAsync(query);
        return query.Result.CreateResultAdd_Msg();
    }

    /// <summary>
    /// 获取媒体项详情
    /// </summary>
    [Authorize]
    [EndpointDescription("获取媒体项详情")]
    [EndpointName("获取媒体项详情")]
    [EndpointSummary("获取媒体项详情")]
    public async Task<ResponseModel<JellyfinItemDetailDto>> GetItemDetail(string id)
    {
        var query = new GetJellyfinItemDetailQuery(id);
        await PublishAsync(query);
        return query.Result.CreateResultAdd_Msg();
    }

    /// <summary>
    /// 获取子项列表
    /// </summary>
    [Authorize]
    [EndpointDescription("获取子项列表")]
    [EndpointName("获取子项列表")]
    [EndpointSummary("获取子项列表")]
    public async Task<ResponseModel<JellyfinItemsDto>> GetChildren(string id)
    {
        var query = new GetJellyfinChildrenQuery(id);
        await PublishAsync(query);
        return query.Result.CreateResultAdd_Msg();
    }

    /// <summary>
    /// 获取媒体流
    /// </summary>
    [Authorize]
    [EndpointDescription("获取媒体流")]
    [EndpointName("获取媒体流")]
    [EndpointSummary("获取媒体流")]
    public async Task<IActionResult> GetStream(string id)
    {
        var query = new GetJellyfinStreamQuery(id);
        await PublishAsync(query);
        return query.Result;
    }

    [Authorize]
    // [HttpGet("Images/{id}/{itemId}/{imageType}")]
    [EndpointDescription("获取图片文件流")]
    [EndpointName("获取图片文件流")]
    [EndpointSummary("获取图片文件流")]
    public async Task<FileContentResult> GetImage(
        string id,
        string itemId,
        string imageType)
    {
        var query = new GetJellyfinImageQuery(id, itemId, imageType);
        await PublishAsync(query);
        return new FileContentResult(query.Result!, "image/jpeg");
    }
}